package com.alkin.thing.project.iot.controller;

import com.alkin.thing.common.utils.ContextUtils;
import com.alkin.thing.common.utils.StringUtils;
import com.alkin.thing.common.utils.poi.ExcelUtil;
import com.alkin.thing.framework.aspectj.lang.annotation.Log;
import com.alkin.thing.framework.aspectj.lang.enums.BusinessType;
import com.alkin.thing.framework.security.LoginUser;
import com.alkin.thing.framework.web.controller.BaseController;
import com.alkin.thing.framework.web.domain.AjaxResult;
import com.alkin.thing.framework.web.page.TableDataInfo;
import com.alkin.thing.project.iot.domain.View;
import com.alkin.thing.project.iot.domain.ViewLink;
import com.alkin.thing.project.iot.service.IViewService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * web可视化页面Controller
 *
 * @author alkin
 * @date 2020-02-25
 */
@RestController
@RequestMapping("/iot/view")
public class ViewController extends BaseController {
    @Autowired
    private IViewService viewService;


    /**
     * 查询web可视化页面列表
     */
    //@PreAuthorize("@ss.hasPermi('busi:view:list')")
    @GetMapping("/list")
    public TableDataInfo list(View view) {
        startPage();
        QueryWrapper<View> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(view.getName()), "name", view.getName());
        List<View> list = viewService.list(queryWrapper);
        for (View item : list) {
            // 页面具体内容太多，不需要查询到列表页
            item.setContent(null);
        }
        return getDataTable(list);
    }

    /**
     * 导出web可视化页面列表
     */
    //@PreAuthorize("@ss.hasPermi('busi:view:export')")
    @Log(title = "web可视化页面", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(View view) {
        QueryWrapper<View> queryWrapper = new QueryWrapper<>();
        List<View> list = viewService.list(queryWrapper);
        ExcelUtil<View> util = new ExcelUtil<View>(View.class);
        return util.exportExcel(list, "view");
    }

    /**
     * 获取web可视化页面详细信息
     */
    //@PreAuthorize("@ss.hasPermi('busi:view:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(viewService.getById(id));
    }


    @GetMapping(value = "/getByBizId")
    public AjaxResult getByBizId(View query) {
        View view = viewService.getByBizTypeAndId(query.getBizType(), query.getBizId());
        if (view != null) {
            return AjaxResult.success(view);
        }
        return AjaxResult.error("未配置监控页面");
    }

    /**
     * 新增web可视化页面
     */
    //@PreAuthorize("@ss.hasPermi('busi:view:add')")
    @Log(title = "web可视化页面", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody View view) {

        if (view.getCopyId() != null) { //复制页面
            View copy = viewService.getById(view.getCopyId());
            view.setContent(copy.getContent());
        }

        viewService.save(view);
        return AjaxResult.success(view);
    }

    /**
     * 修改web可视化页面
     */
    //@PreAuthorize("@ss.hasPermi('busi:view:edit')")
    @Log(title = "web可视化页面", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody View view) {

        LoginUser loginUser = ContextUtils.getLoginUser();
        view.setUpdateBy(loginUser.getUsername());
        view.setUpdateTime(new Date());
        viewService.updateById(view);
        return AjaxResult.success();
    }

    /**
     * 删除web可视化页面
     */
    //@PreAuthorize("@ss.hasPermi('busi:view:remove')")
    @Log(title = "web可视化页面", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        viewService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @PostMapping("link")
    public AjaxResult saveViewLink(@RequestBody ViewLink viewLink) {
        viewService.saveViewLink(viewLink);
        return AjaxResult.success();
    }

    /**
     * 测试组态-交互-接口调用
     *
     * @return
     */
    @PostMapping("testAction")
    public AjaxResult testAction(@RequestBody Map<String, String> map) {
        return AjaxResult.success("操作成功", map);
    }


}
